﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class AwardEdit : System.Web.UI.Page
    {
        int AwardID;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                AwardID = Convert.ToInt32(Request.QueryString["AwardID"].ToString().Trim());
                AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();
                DataTable dttbl = dp.Get_tbl_Award_Select_All_By_AwardID(AwardID);
                this.txtAwardName.Text = dttbl.Rows[0]["AwardName"].ToString();
                this.imgAwardImage.ImageUrl = "~/images/" + dttbl.Rows[0]["AwardImage"].ToString();
                this.txtAwardDescription.Value = Server.HtmlDecode(dttbl.Rows[0]["AwardDescription"].ToString());

                foreach (ListItem item in this.cbbAwardStatus.Items)
                {
                    if (item.Value == dttbl.Rows[0]["AwardStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
            catch
            {
                //show error here

            }
        }

        /// <summary>
        /// nút Update được ấn
        /// </summary>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                //string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                //string strUserID = Membership.GetUser(strUser).ProviderUserKey.ToString();
                //Guid userID = new Guid(strUserID);

                string strUser = this.cbbSponsor.SelectedValue.ToString();
                Guid userID = new Guid(strUser);

                string strImageSource;
                if (this.fileUpAwardImage.HasFile)
                {
                    string str = DateTime.Now.ToString("ddMMyyyyhhmmssms");
                    this.fileUpAwardImage.SaveAs(Server.MapPath("~/images/" + str +
                        this.fileUpAwardImage.FileName.Trim()));
                    strImageSource = str + this.fileUpAwardImage.FileName.Trim();
                }
                else
                {
                    strImageSource = this.imgAwardImage.ImageUrl.Replace("~/images/", "");
                }

                AwardID = Convert.ToInt32(Request.QueryString["AwardID"].ToString().Trim());
                AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();
                int ok = dp.tbl_Award_Update(Convert.ToInt32(this.cbbExam.SelectedValue.Trim()),
                    userID,
                    this.txtAwardName.Text.Trim(), strImageSource,
                    Server.HtmlEncode(this.txtAwardDescription.Value.Trim()),
                    Convert.ToInt32(this.cbbAwardStatus.SelectedValue.Trim()), AwardID);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                AwardID = Convert.ToInt32(Request.QueryString["AwardID"].ToString().Trim());
                AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();

                int ok = dp.tbl_Award_Delete(AwardID);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog_Del(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog_Del(FAIL);
            }
        }

        /// <summary>
        /// Đóng modal popup
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Award.aspx");
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// show dialog
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// show dialog neu an xoa
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        /// <summary>
        /// combobox chứa các kỳ thi đã được bind dữ liệu xong
        /// </summary>
        protected void cbbExam_DataBound(object sender, EventArgs e)
        {
            try
            {
                AwardID = Convert.ToInt32(Request.QueryString["AwardID"].ToString().Trim());
                AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();
                DataTable dttbl = dp.Get_tbl_Award_Select_All_By_AwardID(AwardID);

                foreach (ListItem item in this.cbbExam.Items)
                {
                    if (item.Value == dttbl.Rows[0]["ExamID"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
            catch
            {
                //show error here
            }
        }

        /// <summary>
        /// combobox chứa các nhà tài trợ đã được bind dữ liệu xong
        /// </summary>
        protected void cbbSponsor_DataBound(object sender, EventArgs e)
        {
            try
            {
                AwardID = Convert.ToInt32(Request.QueryString["AwardID"].ToString().Trim());
                AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();
                DataTable dttbl = dp.Get_tbl_Award_Select_All_By_AwardID(AwardID);

                foreach (ListItem item in this.cbbSponsor.Items)
                {
                    if (item.Value == dttbl.Rows[0]["SponsorID"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
            catch
            {
                //show error here
            }
        }
    }
}
